/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client;

import javax.inject.Inject;
import javax.inject.Named;

import org.kad.gwt.web.client.inject.GWTKadGinjector;
import org.kad.gwt.web.client.kademelia.inject.KadModule;
import org.kad.gwt.web.client.kademelia.model.KadNode;
import org.kad.gwt.web.client.kademelia.transport.mvp.KadRequestEvent;
import org.kad.gwt.web.client.transport.http.mvp.error.HttpServerErrorEvent;
import org.kad.gwt.web.client.transport.http.mvp.message.HttpServerMessageEvent;
import org.kad.gwt.web.client.transport.http.mvp.ready.HttpServerReadyEvent;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

/**
 * Root entry point.
 * 
 * @author Pierrick HYMBERT &lt;pierrick [dot] hymbert [at] gmail [dot] com&gt;
 */
public class GWTKad implements EntryPoint {
    /**
     * The gwt kad server port.
     */
    public static final int GWT_KAD_PORT_START = 50505;
    /**
     * The application dependency injector.
     */
    final GWTKadGinjector   injector           = GWT.create(GWTKadGinjector.class);

    public static class Tmp {
        @Inject
        @Named(KadModule.KAD_LOCAL_NODE)
        public KadNode localNode;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onModuleLoad() {
        injector.getEventBus().addHandler(HttpServerErrorEvent.getType(), new HttpServerErrorEvent.Handler() {
            @Override
            public void onHttpServerError(HttpServerErrorEvent event) {
                System.err.println(event.getErrorMessage());
                Window.alert("GWT - Server error : " + event.getErrorMessage());
            }
        });
        injector.getEventBus().addHandler(HttpServerMessageEvent.getType(), new HttpServerMessageEvent.Handler() {
            @Override
            public void onHttpServerMessage(HttpServerMessageEvent event) {
                System.out.println("GWT - Server message " + event.getRequest().getUri() + " - " + event.getRequest().getMessage());
            }
        });
        injector.getEventBus().addHandler(HttpServerReadyEvent.getType(), new HttpServerReadyEvent.Handler() {
            @Override
            public void onHttpServerReady(HttpServerReadyEvent event) {
                System.out.println("GWT - HTTP Server ready");
            }
        });
        injector.getEventBus().addHandler(KadRequestEvent.getType(), new KadRequestEvent.Handler() {
            @Override
            public void onKadRequest(KadRequestEvent event) {
                System.out.println("GWT - KAD message " + event.getRequest() + " ==> " + event.getResponse());
            }
        });
        injector.getHttpAppletServer().start();
    }
}
